/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import entities.New;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 *
 * @author Thanh
 */
public class GetNewsDAO {

    public ArrayList<New> getNews() {
        ArrayList<New> results = new ArrayList<New>();
        try {
            String un = "sa";
            String pw = "1234";
            String url = "jdbc:sqlserver://localhost:1433;databaseName=BloggerDB;";
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection con = java.sql.DriverManager.getConnection(url, un, pw);

            String sql = "select n.NewID, n.Title, n.Content, n.CreatedTime, u.Username, u.FullName from News n left join Users u on n.UserID = u.UserID";
            PreparedStatement stm = con.prepareStatement(sql);
            ResultSet rs = stm.executeQuery();

            while (rs.next()) {
                New neww = new New();
                neww.setNewID(rs.getInt("NewID"));
                neww.setTitle(rs.getString("Title"));
                neww.setContent(rs.getString("Content"));
                neww.setCreatedTime(rs.getDate("CreatedTime").toString() + " " + rs.getTime("CreatedTime").toString());
                neww.setUsername(rs.getString("Username"));
                neww.setFullName(rs.getString("FullName"));
                results.add(neww);
            }
            rs.close();
            stm.close();
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return results;
    }
}
